<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%String path = request.getContextPath();%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>
<%@ taglib uri="/WEB-INF/tld/sys.tld" prefix="sys" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>客户身份证附件上传</title>
    <style type="text/css">
    </style>
    <jsp:include page="../../import.jsp" flush="true"/>
    <script type="text/javascript" src="//cdn.staticfile.org/webuploader/0.1.5/webuploader.min.js" charset="utf-8">
    </script>
    <script src="//cdn.bootcss.com/blueimp-md5/2.10.0/js/md5.min.js"></script>
</head>

<body class="iframeBody">
<div class="Wrapper">
    <div class="fieldset1 tabel_col tabel_col_s clearFix">
        <div class="sqPanel">
            <div class="sqPanel_head inL">
                <span>客户身份证资料</span>
            </div>
            <div class="sqPanel_body">
                <div id="cstFileInfoCfgGrid" class="sq-grid"></div>
            </div>
        </div>
    </div>
    <!-- 资料上传DIV -->
    <div id="prj_file_div" style="display:none">
        <form id="prj_file_formSet">
            <div class="fieldset1 tabel_col noBorder clearFix">
                <div class="fieldBox col_100">
                    <jsp:include page="../../system/sysUlFilesRec/sysSysUlFilesRecUpload.jsp" flush="true" >
                        <jsp:param name="fileNum" value="1"/>
                        <jsp:param name="filePath" value="prjFileInfo"/>
                    </jsp:include>
                </div>
            </div>
            <input type="text" style="display: none;" id="cfgCd" name="cfgCd">
            <input type="text" style="display: none;" id="cfgDtlCd" name="cfgDtlCd">
            <input type="text" style="display: none;" id="orgCd" name="orgCd">
            <input type="text" style="display: none;" id="dataId" name="dataId" value="${dataId}">
        </form>
    </div>
    <script type="text/javascript">
        var cstFileInfoCfgGrid = null;//客户资料配置明细
        $(function () {
            //客户资料配置明细Grid
            cstFileInfoCfgGrid = $("#cstFileInfoCfgGrid").ligerGrid({
                url: '<%=path%>/project/prjFileInfo/initCstFileInfoCertNoList.do?cstId=${dataId}',
                method: 'get',
                columns: [
                    {display: '资料名称', name: 'datNm',align: 'left', minWidth: 100, enabledSort: false},
                    {display: '上传文件名称', name: 'fileNm',align: 'left', minWidth: 100, enabledSort: false,
                        render:function(rowdata,rowindex,value){
                            if(value){
                                var h;
                                h='<a href="javascript:uploadFileByUrl(';
                                h+="'"+rowdata.filePath+"'";
                                h+=",'"+rowdata.fileNm+"'";
                                h+=')">'+rowdata.fileNm+'</a>'
                                return h;
                            }
                        }
                    },
                    {display: '上传时间', name: 'crtTm',align: 'left', minWidth: 100, enabledSort: false,render:renderDate},
                    {display: '状态', name: 'audStsCdNm', width: 100, enabledSort: false},
                    {display: '操作', name: '', minWidth: 100,align: 'left',  enabledSort: false,
                        render:function(rowdata,rowindex,value){
                            var h='';
                                h += '<a class="sq-button-hasicon" style="cursor:pointer;" onclick="delFile(event,' + rowdata.fileId + ');"><i class="sqBtnIco_7"></i>删除</a>';
                            return h;
                        }
                    },
                    {display: '上传', name: 'uploadNm', width: 80,align: 'left', enabledSort: false,
                        render:function(rowdata,rowindex,value){
                            var h='';
                                h += '<span class="sq-button sq-button-round fileupload-button">上传<input type="file" id="fileup_' + rowdata.cfgCd + '_' + rowdata.cfgDtlCd + '" multiple name="fileup_' + rowdata.cfgCd + '_' + rowdata.cfgDtlCd + '"' +
                                    ' class="fileupload-input" onchange="onChangefile_multi(event,\'' + rowdata.cfgCd + '\',\'' + rowdata.cfgDtlCd + '\',\'' + rowdata.orgCd + '\',1)"></span>';
                            return h;
                        }
                    },
                ],
                root: 'rows',
                hideLoadButton : true,
                width: '100%',
                height: '100%',
                isScroll: false,
                rownumbers:true,  //显示序号
                checkbox: false,
                allowUnSelectRow:true,
                alternatingRow: false,
                frozen: false,
                usePager: false,
                onAfterShowData: function ()
                {
                    $('#cstFileInfoCfgGrid').rowspan('datNm');
                    $('#cstFileInfoCfgGrid').rowspan('uploadNm');
                },
            });
            $("#pageloading").hide();
        });

        function onChangefile_multi(e,cfgCd,cfgDtlCd,orgCd,typ) {
            var wait_dialog = $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING, SQ_type: "waitting"});
            var filePath="";
            if(typ==1){
                filePath="customer";
            }
            $.ajaxFileUpload({
                url: '<%=path%>/system/sysulfilesrec/upload.do',
                secureuri: false,
                fileElementId: e.target.id,  //这里不在是以前的id了，要写成数组的形式！
                dataType: 'json',
                data: {filePath: filePath},
                success: function (msg) {
                    if (msg.success == 1) {
                        if(typ==1){
                            $.ajax({
                                type: 'post',
                                dataType: 'json',
                                data: {'fileParams':JSON.stringify(msg.data),'cstId':$("#dataId").val(),'cfgCd':cfgCd,'cfgDtlCd':cfgDtlCd,'orgCd':orgCd},
                                url: '<%=path%>/project/prjFileInfo/uploadCstFile.do',
                                success: function (msg) {
                                    if (msg.success == 1) {
                                        cstFileInfoCfgGrid.reload();
                                    }else{
                                        $.ligerDialog.SQalert({content: "资料上传超时", SQ_type: "error"});
                                    }
                                    $.ligerDialog.hide();
                                }
                            });
                        }
                    } else {
                        wait_dialog.close();
                        $.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
                    }
                }
            });
        };

        $(function () {
            $(document).on('click', 'a[name=showimg]', function(){
                event.preventDefault();
                var href = $(this).attr('href');
                reg = new RegExp('^.*\.(?:png|jpg|bmp|gif|jpeg|PNG|JPG|BMP|GIF|JPEG)$');
                if (reg.test(href)) {
                    SQ.imgPreview(href);
                }else{
                    window.open(href);
                }
            });
        });

        //格式化时间
        function renderDate(rowdata, index, value) {
            var dateStr = "";
            if (null != value) {
                var date = new Date(value);
                dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DDHHMMSS);
            }
            return dateStr;
        }

        var reloadChildGridId = null;
        var callbackFlag = 3;
        //上传客户文件
        function upFileForCst(event,_cfgCd,_cfgDtlCd,_orgCd){
            //获取子GridID
            event.stopPropagation();
            reloadChildGridId = $(event.target).parents('.sq-grid').attr('ligeruiid');

            callbackFlag = 1;
            $("#cfgCd").val(_cfgCd);
            $("#cfgDtlCd").val(_cfgDtlCd);
            $("#orgCd").val(_orgCd);
            $.ligerDialog.SQopen({
                width: 400,
                height:100,
                title : '资料上传',
                target: $("#prj_file_div"),
            });
        }
        //上传客户文件回调方法
        function uploadCallback_2(_data){
            if(callbackFlag==1){
                $.ajax({
                    type: 'post',
                    dataType: 'json',
                    data: {'fileParams':JSON.stringify(_data),'cstId':$("#dataId").val(),'cfgCd':$("#cfgCd").val(),'cfgDtlCd':$("#cfgDtlCd").val(),'orgCd':$("#orgCd").val()},
                    url: '<%=path%>/project/prjFileInfo/uploadCstFile.do',
                    success: function (msg) {
                        if (msg.success == 1) {
                            parent.$('li[tabid="mateTab"] a').text('客户资料('+msg.data+')');
                            /* if(cstFileInfoGrid){
                                cstFileInfoGrid.reload();
                            } */
                            if(reloadChildGridId) liger.get(reloadChildGridId).reload();
                        }else{
                            $.ligerDialog.SQalert({content: "资料上传超时", SQ_type: "error"});
                        }
                        $.ligerDialog.hide();
                    }
                });
            }
        }

        //删除文件
        function delFile(event,_fileId){
            top.$.ligerDialog.confirm("确认删除吗?", function (flag) {
                if(!flag){
                    return;
                }else{
                    var wait_dialog;
                    $.ajax({
                        type: 'post',
                        dataType: 'json',
                        data: {'fileId':_fileId,'marks':1},
                        url: '<%=path%>/project/prjFileInfo/removeFile.do',
                        beforeSend: function () {
                            wait_dialog = $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING, SQ_type: "waitting"});
                        },
                        success: function (msg) {
                            wait_dialog.close();
                            if (msg.success == 1) {
                                $.ligerDialog.SQalert({content: msg.info, SQ_type: "success"});
                                if(reloadChildGridId) liger.get(reloadChildGridId).reload();
                            }else{
                                $.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
                            }
                        }
                    });
                }
            });
        }

        function downFile(_url){
            //window.location.href=_url;
            window.open(_url);
        }
        function uploadFileByUrl(_url,_name){
            if(SQ.regs.img.test(_name)){
                //SQ.imgPreview(_url);
                //SQ.imgPreview(_url, true);
                window.__ImgSrc = _url;
                window.__ImgTit = _name;
                window.open('<%=path%>/project/prjbscinfo/imgWinOpen.do', '', 'location=yes, width=800, height=600');
            }else{
                window.open(_url);
            }
        }

        var opts = {cfgCd:"",cfgDtlCd:"",orgCd:""};

        function makeUploaders(gridNm, typ) {
            var filePath="";
            if(typ==1){
                filePath="/customer/";
            }
            var userInfo = {userId:"${currentUsrId}", md5:"", filePath:filePath};
            var chunkSize = 5000 * 1024;        //分块大小
            var originFileName = null;          //原始文件名称
            var uniqueFileName = null;          //文件唯一标识符
            var fileExt = null;
            var md5Mark = null;

            var backEndUrl = '<%=path%>/system/sysulfilesrec/tillFileUpload.do';
            var backStatusUrl = '<%=path%>/system/sysulfilesrec/tillFileUploadStatus.do';
            var uploader = WebUploader.create({
                swf: "Uploader.swf"
                , server: backEndUrl
                , resize: false
                , compress: false
                , prepareNextFile: true
                , chunked: true
                , chunkSize: chunkSize
                , threads: 3
                , formData: $.extend(true, {}, userInfo)
                , fileNumLimit: 1
                //, fileSingleSizeLimit: 1000 * 1024 * 1024
                , duplicate: true
            });
            uploader.on("fileQueued", function(file){
                var selectedRowId = file.source._refer.parents('.l-grid-row').attr('id');
                var rowId = selectedRowId.slice(selectedRowId.lastIndexOf("|")+1);
                var rowData = liger.get(gridNm).getRow(rowId);
                opts = {cfgCd:rowData.cfgCd,cfgDtlCd:rowData.cfgDtlCd,orgCd:rowData.orgCd};
                var wait_dialog = $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING, SQ_type: "waitting"});
                uploader.upload();
            });
            uploader.on("uploadStart", function(file){
                originFileName = file.name;
                fileExt = file.ext;
                uniqueFileName = md5(''+userInfo.userId+file.name+file.type+file.lastModifiedDate+file.size+new Date().getTime());
                md5Mark = uniqueFileName;
                userInfo.md5 = uniqueFileName;
            });
            uploader.on('uploadBeforeSend', function(block, data, headers) {
                // 修改data可以控制发送哪些携带数据。
                data.originFileName = originFileName;
                data.fileName = uniqueFileName;
                data.ext = fileExt;
                //分片验证是否已传过，用于断点续传
                var task = new $.Deferred();
                $.ajax({
                    type: "POST"
                    , url: backStatusUrl
                    , data: {
                        status: "chunkCheck"
                        , originFileName: originFileName
                        , fileName: uniqueFileName
                        , chunkIndex: block.chunk
                        , size: block.end - block.start
                        , filePath:filePath
                    }
                    , cache: false
                    , timeout: 5000 //todo 超时的话，只能认为该分片未上传过
                    , dataType: "json"
                }).then(function(data, textStatus, jqXHR){
                    if(data.success == 2){   //若存在，返回失败给WebUploader，表明该分块不需要上传
                        task.resolve();
                    }else{
                        task.reject();
                    }
                }, function(jqXHR, textStatus, errorThrown){    //任何形式的验证失败，都触发重新上传
                    task.resolve();
                });
                return $.when(task);
            });
            uploader.on("uploadComplete", function(file){
                var chunksTotal = 0;
                if((chunksTotal = Math.ceil(file.size/chunkSize)) > 1){
                    //合并请求
                    var task = new $.Deferred();
                    $.ajax({
                        type: "POST"
                        , url: backStatusUrl
                        , data: {
                            status: "chunksMerge"
                            , originFileName: originFileName
                            , fileName: uniqueFileName
                            , chunks: chunksTotal
                            , ext: file.ext
                            , md5: md5Mark
                            , filePath:filePath
                        }
                        , cache: false
                        , dataType: "json"
                    }).then(function(data, textStatus, jqXHR){
                        task.resolve();
                        if(data.success==1 && Math.ceil(file.size/chunkSize)>1){
                            if(typ==1){
                                $.ajax({
                                    type: 'post',
                                    dataType: 'json',
                                    data: {'fileParams':JSON.stringify(data.data),'cstId':$("#dataId").val(),'cfgCd':opts.cfgCd,'cfgDtlCd':opts.cfgDtlCd,'orgCd':opts.orgCd},
                                    url: '<%=path%>/project/prjFileInfo/uploadCstFile.do',
                                    success: function (msg) {
                                        if (msg.success == 1) {
                                            cstFileInfoCfgGrid.reload();
                                        }else{
                                            $.ligerDialog.SQalert({content: "资料上传超时", SQ_type: "error"});
                                        }
                                        $.ligerDialog.hide();
                                    }
                                });
                            }
                        }else if(data.success!=1) {
                            //wait_dialog.close();
                            $.ligerDialog.SQalert({content: data.info, SQ_type: "error"});
                        }
                        uploader.removeFile(file);//上传成功后移除队列文件
                    }, function(jqXHR, textStatus, errorThrown){
                        task.reject();
                    });
                    uploader.removeFile(file);//上传成功后移除队列文件
                    return $.when(task);
                }else{
                    uploader.removeFile(file);//上传成功后移除队列文件
                }
            });
            uploader.on('uploadAccept', function(obj, ret) {
                if(ret.success==1 && Math.ceil(obj.file.size/chunkSize)<=1){
                    if(typ==1){
                        $.ajax({
                            type: 'post',
                            dataType: 'json',
                            data: {'fileParams':JSON.stringify(ret.data),'cstId':$("#dataId").val(),'cfgCd':opts.cfgCd,'cfgDtlCd':opts.cfgDtlCd,'orgCd':opts.orgCd},
                            url: '<%=path%>/project/prjFileInfo/uploadCstFile.do',
                            success: function (msg) {
                                if (msg.success == 1) {
                                    cstFileInfoCfgGrid.reload();
                                }else{
                                    $.ligerDialog.SQalert({content: "资料上传超时", SQ_type: "error"});
                                }
                                $.ligerDialog.hide();
                            }
                        });
                    }
                }
            });
            return uploader;
        }

    </script>

</body>
</html>
